package com.lanou.demo.backgrounddemo.controller;

import com.lanou.demo.backgrounddemo.service.ContentService;
import com.lanou.entity.entity.ContentEntity;
import com.lanou.entity.utils.AjaxResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 常宏盛
 * Date: 2019/7/15 10:12
 **/
@RestController
public class ContentController {

    @Resource
    private ContentService contentService;

    /**
     * 内容管理分页查询
     *
     * @param categoryId
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/content/query/list")
    public List<ContentEntity> list(Long categoryId, int page, int rows) {
        return this.contentService.list(categoryId, page, rows);
    }

    /**
     * 内容新增
     *
     * @param content
     * @return
     */
    @RequestMapping("/content/save")
    public AjaxResult save(ContentEntity content) {
        int saveResult = this.contentService.save(content);
        if (saveResult == 0) {
            return AjaxResult.builder().status(400).build();
        }
        return AjaxResult.builder().status(200).build();
    }

    /**
     * 内容修改
     *
     * @param content
     * @return
     */
    @RequestMapping("/rest/content/edit")
    public AjaxResult edit(ContentEntity content) {
        int editResult = this.contentService.edit(content);
        if (editResult == 0) {
            return AjaxResult.builder().status(400).build();
        }
        return AjaxResult.builder().status(200).build();
    }

    /**
     * 内容删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/content/delete")
    public AjaxResult delete(String[] ids) {
        int deleteResult = this.contentService.delete(ids);
        if (deleteResult == 0) {
            return AjaxResult.builder().status(400).build();
        }
        return AjaxResult.builder().status(200).build();
    }

}

